package com.mdb.open.trade.service.trade;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdb.bc.cache.sequence.AutoIncrementInitializer;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.po.trade.MerAcc;
import com.mdb.open.trade.mapper.trade.TfMerAccMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: you.yichao
 * @create: 2019-09-02 18:12
 **/
@Component
@DS(MdbDataSource.TRADE)
public class MerSubAccAutoIncrementInitializer implements AutoIncrementInitializer {

    @Autowired
    TfMerAccMapper tfMerAccMapper;

    @Override
    public long getInitialValue() {

        MerAcc acc = tfMerAccMapper.selectOne(new LambdaQueryWrapper<MerAcc>().orderByDesc(MerAcc::getId).last("limit 1"));
        if (ObjectUtil.isEmpty(acc)) {
            return 0;
        }

        String accNo = StrUtil.subSufByLength(acc.getPlatformAcc(), 8);
        return NumberUtil.parseLong(accNo);
    }

}
